package com.transpeed.payservice.pay.unionpospay;

import com.transpeed.payservice.log.Log;
import com.transpeed.payservice.main.PostMessage;
import com.transpeed.payservice.model.PosLocalCarInfo;

public class PosMain extends Thread {
	private String TotalAmount;
	private String NotifyUrl;
	private String PosTradeNumber;

	public PosMain(String TotalAmount, String NotifyUrl, String PosTradeNumber) {
		this.TotalAmount = TotalAmount;
		this.NotifyUrl = NotifyUrl;
		this.PosTradeNumber = PosTradeNumber;
	}

	@Override
	public void run() {
		super.run();

		Log.Write("POS机刷卡已启动！", "LocalPos");
		String PosStatusCode = null;// 初始化Pos机状态码
		String PosTradeStatus = "Failed"; // 初始化POS交易状态
		PosLocalCarInfo.LocalPosStatusSign = false;// 初始化本地POS机状态标识

		// PosStatusCode =
		// PosTrade.Consume(TotalAmount,NotifyUrl,PosTradeNumber);
		for (int i = 0; i < 5; i++) {
			PosStatusCode = PosTrade.Consume(TotalAmount);
			Log.Write("第" + (i + 1) + "次刷卡已完成！状态码为：" + PosStatusCode,
					"LocalPos");

			if (PosStatusCode.equals("00"))// 成功
			{
				PosTradeStatus = "UmsPayPos&Success&" + PosTradeNumber;
				Log.Write("交易成功！交易状态为：" + PosTradeStatus, "LocalPos");
				PosLocalCarInfo.LocalPosStatusSign = true;
				break;
			}

			if (PosStatusCode.equals("Y3"))// 超时
			{
				PosTradeStatus = "UmsPayPos&Falied&" + PosTradeNumber;
				Log.Write("交易失败！交易状态为：" + PosTradeStatus, "LocalPos");
				break;
			} else
			// 其他情况都为失败
			{
				PosTradeStatus = "UmsPayPos&Falied&" + PosTradeNumber;
				Log.Write("交易失败！交易状态为：" + PosTradeStatus, "LocalPos");
			}
		}

		Log.Write("准备向上位机推送刷卡信息！信息为：" + PosTradeStatus, "LocalPos");
		//NotifyUrl = "192.168.0.102:8080/transpeed-PayService/TestWebDaoImpl/Test"; // 测试
		Log.Write("推送上位机地址为：" + NotifyUrl, "LocalPos");

		String info = PostMessage.PosPost(PosTradeStatus, NotifyUrl, "LocalPos");// 向上位机推送消息
		if (info.equals("1"))
			Log.Write("向上位机传送信息成功！！！", "LocalPos");

		String PosTradeinfo = PosTradeStatus + "&" + PosLocalCarInfo.LocalPosStatusSign;// 传给服务器保存的交易信息
		Log.Write("准备向服务器推送刷卡信息！信息为：" + PosTradeinfo, "LocalPos");
		String PosServerNotify = "http://192.168.254.5:8080/transpeed-PayService/PosTradeAPI/PosServerReceive";
		
		Log.Write("服务器推送地址为：" + PosServerNotify, "LocalPos");
		String Serverinfo = PostMessage.PosPost(PosTradeinfo, PosServerNotify,"LocalPos");// 向服务器推送消息
		
		if (Serverinfo.equals("1"))
			Log.Write("向服务器上传信息成功！！！", "LocalPos");
	}
}
